<?php

    namespace Admin\Controller;

    use Common\Controller\AdminbaseController;

    class AdminRechargeController extends AdminbaseController {

        protected $m_logistics;
        protected $m_recharge;
        protected $m_user;

        function _initialize() {
            parent::_initialize();
            $this->m_logistics= D("Admin/Logistics");
            $this->m_recharge= D("Admin/Recharge");
            $this->m_user = D("Common/Users");
        }

        /**
         * 充值记录
         */
        public function index(){

            $s_rid = I("request.s_rid");
            $this->assign("s_rid",$s_rid);
            if(!empty($s_rid)){
                $where['r.rid'] =$s_rid;
            }else{
                $lid = get_logistics_id();
                if($lid){
                    $lids = get_logistics_ids();
                    $where['r.rid'] = array('in',$lids);
                }else{
                    $where = array();
                }
            }

            $admin_list = $this->m_user->alias('u')
            ->field('u.*, l.logistics_name')
            ->join('bdt_logistics l on l.id = u.organize_id','left')
            ->where(array('model'=>array('in','2,4,5,8,9,10,11,12,13')))
            ->select();
            $this->assign("admin_list",$admin_list);

            $s_admin_id = I("request.s_admin_id");
            $this->assign("s_admin_id",$s_admin_id);
            if(!empty($s_admin_id)){
                $where['r.uid'] = $s_admin_id;
            }

            $s_status = I("request.s_status");
            $this->assign("s_status",$s_status);
            if(!empty($s_status)){
                $where['r.status'] = $s_status;
            }else{
                $where['r.status'] = array('neq',0);
            }

            $where["r.model"] = array('in','1,4');

            $s_start_time = I("s_start_time");
            $s_end_time = I("s_end_time");
            if(empty($s_start_time)){
                $s_start_time = strtotime(date("Y-m-d 00:00:00")." -7 day");
            }else{
                $s_start_time = strtotime($s_start_time.":00");
            }
            if(empty($s_end_time)){
                $s_end_time = time();
            }else{
                $s_end_time = strtotime($s_end_time.":59");
            }
            $this->assign("s_start_time",$s_start_time);
            $this->assign("s_end_time",$s_end_time);
            $where["r.create_time"]=array("BETWEEN",array($s_start_time,$s_end_time));

            $count=$this->m_recharge->alias("r")->where($where)->count();
            $page = $this->page($count, 15);
            if (I('s_download') != 1) {
                $list = $this->m_recharge->get_list($where, "r.create_time DESC", $page->firstRow, $page->listRows);
            }else{
                $list = $this->m_recharge->get_list($where, "r.create_time DESC", $page->firstRow, $count);
                foreach($list AS &$item){
                    if($item["status"] == 1){
                        $item["status_show"] = "待审核";
                    }elseif($item["status"] == 2 && $item['model'] == 1){
                        $item["status_show"] = "后台充值成功";
                    }elseif($item["status"] == 2 && $item['model'] == 4){
                        $item["status_show"] = "上级物流公司充值成功";
                    }elseif($item["status"] == 3 && $item['model'] == 1){
                        $item["status_show"] = "充值失败";
                    }elseif($item["status"] == 3 && $item['model'] == 4){
                        $item["status_show"] = "上级物流公司充值失败";
                    }
                }
                $export_title = array(
                    'user_nicename' => '操作用户',
                    'user_phone' => '用户电话',
                    'logistics_name' => '物流公司名称',
                    'price' => '充值金额',
                    'after_balance' => '充值后余额',
                    'general_price' => '赠送金额',
                    'status_show' => '状态',
                    'create_time' => '创建时间',
                    'desc' => '充值描述',
                );

                $excel = new \Common\Lib\excel();
                $excel->export_data('物流公司充值记录', $export_title, $list);
                exit;
            }

            $logistics_list = $this->m_logistics->get_list();
            $this->assign("logistics_list",$logistics_list);
            $this->assign("page", $page->show('Admin'));
            $this->assign("list",$list);
            $this->display();
        }

        // 添加物流公司
        public function add(){
            $id = I("get.id",0,'intval');
            // $logistics_list = $this->m_logistics->get_list();
            // $this->assign("logistics_list",$logistics_list);
            if(!empty($id)){
                $item=M("Logistics")->where(array('id'=>$id))->find();
                $this->assign("item",$item);
            }

            $this->display();
        }

        // 充值二级物流公司
        public function add_sub(){
            $id = I("get.id",0,'intval');
            // $logistics_list = $this->m_logistics->get_list();
            // $this->assign("logistics_list",$logistics_list);
            if(!empty($id)){
                $item = M("Logistics")->where(array('id'=>$id))->find();
                $upper = M("Logistics")->where(array('id'=>$item['manager_id']))->find();

                $this->assign("item",$item);
                $this->assign("upper",$upper);
            }

            $this->display();
        }

        // 提交
        public function add_post(){
            if (IS_POST) {
                $insert = I("post.");

                $result = $this->m_recharge->insert_data($insert,1);
                if ($result) {
                    $this->success("操作成功，已进入审核流程！");
                } else {
                    $this->error("操作失败！");
                }

            }
        }

        // 提交
        public function add_sub_post(){
            if (IS_POST) {
                $insert = I("post.");
                $lid = I("post.lid");
                $price = I("post.price");

                $result = $this->m_recharge->insert_data($insert,4);
                if ($result > 0) {
                    $this->success("操作成功！");
                }elseif ($result == -3) {
                    $this->error("上级消费机构余额不足！");
                }else {
                    $this->error("操作失败！");
                }

            }
        }

        // 删除
        public function delete(){
            if(isset($_GET['id'])){
                $id = I("get.id",0,'intval');
                if ($this->m_recharge->where(array('id'=>$id))->delete() !==false) {
                    $this->success("删除成功！");
                } else {
                    $this->error("删除失败！");
                }
            }else{
                $this->error("参数有误！");
            }

        }

        /**
         * 审核
         */
        public function op(){
            $recharge_id = I("request.recharge_id");
            if(empty($recharge_id)){
                $this->error("参数为空！");
            }
            $status = I("request.status");
            if(!in_array($status,array(2,3))){
                $this->error("参数有误！");
            }
            $result = $this->m_recharge->insert_data(array("recharge_id"=>$recharge_id),1,$status);

            if($result == true){
                $this->success("操作成功！");
            }else{
                $this->error("操作失败！");
            }
        }




    }